package com.kobeliu.utils;

import com.google.gson.Gson;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.result.ResponseStatusEnum;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import javax.crypto.SecretKey;
import java.util.Date;


@Component
@Slf4j
public class JwtUtils{

    @Autowired
    private JwtProperties jwtProperties;

    public static final String AT = "@";

//    @Value("${jwt.key}")
//    private String key;

    public String createJwtWithPrefix(String body,Long expired,String prefix){
        if(expired==null) GraceException.display(ResponseStatusEnum.SYSTEM_NO_EXPIRE_ERROR);
        return prefix+AT+createJwt(body,expired);
    }

    public String createJwtWithPrefix(String body,String prefix){
        return prefix+AT+createJwt(body);
    }

    public String createJwt(String body){
        return dealJwt(body,null);
    }

    public String createJwt(String body,Long expired){
        if(expired==null) GraceException.display(ResponseStatusEnum.SYSTEM_NO_EXPIRE_ERROR);
        return dealJwt(body,expired);
    }

    public String dealJwt(String body,Long expired){

        //1.对密钥进行base64编码
        String base64 = new BASE64Encoder().encode(jwtProperties.getKey().getBytes());
        //String base64 = new BASE64Encoder().encode(key.getBytes());
        //2.对base64生成一个密钥的对象
        SecretKey secretKey = Keys.hmacShaKeyFor(base64.getBytes());
        //3.通过JWT生成一个Token字符串

        String jwt = "";
        if(expired !=null){
            jwt = generatorJWTExpired(body,expired,secretKey);
            log.info("生成的JWT为:{},================",jwt);
            return jwt;
        }
        jwt = generatorJWT(body,secretKey);
        log.info("生成的JWT为:{},================",jwt);
        return jwt;
    }

    public String checkJwt(String jwt){

        String base64 = new BASE64Encoder().encode(jwtProperties.getKey().getBytes());
//        String base64 = new BASE64Encoder().encode(key.getBytes());
        SecretKey secretKey = Keys.hmacShaKeyFor(base64.getBytes());

        //校验Jwt
        JwtParser jwtParser = Jwts.parserBuilder()
                .setSigningKey(secretKey)
                .build();   //构造解析器

        //解析
        Jws<Claims> claimsJws = jwtParser.parseClaimsJws(jwt);

        //若此处抛出异常，则Token被篡改
        String subject = claimsJws.getBody().getSubject();

        return subject;



    }

    /**
     * 生成一个Jwt令牌，无限时
     * @param body
     * @param secretKey
     * @return
     */
    public String generatorJWT(String body,SecretKey secretKey){
        return Jwts.builder()
                .setSubject(body) //设置自定义数据
                .signWith(secretKey) //使用具体那个密钥进行生成
                .compact();
    }

    /**
     * 生成一个Jwt限时令牌
     * @param body
     * @param secretKey
     * @return
     */
    public String generatorJWTExpired(String body,Long expiredTimes,SecretKey secretKey){
        return Jwts.builder()
                .setSubject(body) //设置自定义数据
                .signWith(secretKey) //使用具体那个密钥进行生成
                //定义过期时间
                .setExpiration(new Date(System.currentTimeMillis()+expiredTimes))
                .compact();
    }




}
